package com.gy.smart.handle;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gy.smart.R;
import com.gy.smart.base.BaseActivity;
import com.gy.smart.base.BaseBean;
import com.gy.smart.handle.adapter.HandleListAdapter;
import com.gy.smart.handle.bean.HandleListBean;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.StringConstants;
import com.gy.smart.util.Urls;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 17/1/4
 * 说明：办事指南列表
 */
public class HandleListActivity extends BaseActivity {

    private TextView tvSearch;
    private PullToRefreshListView listView;
    private HandleListAdapter adapter;
    private List<HandleListBean> mDatas, requestList;
    private int page = 1,section_id,theme_id;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_handle_list);
        setTitleMsg("办事指南");
        tvSearch = (TextView) findViewById(R.id.fg_handle_list_tv_search);
        listView = findView(R.id.activity_handle_list_listview);
    }

    @Override
    protected void initData() {
        section_id = getIntent().getIntExtra("section_id",0);
        theme_id = getIntent().getIntExtra("theme_id",0);
        mDatas = new ArrayList<>();
        requestList = new ArrayList<>();
        adapter = new HandleListAdapter(this, mDatas);
    }

    @Override
    protected void setListener() {
        super.setListener();
        tvSearch.setOnClickListener(this);
        listView.setMode(PullToRefreshBase.Mode.DISABLED);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(HandleListActivity.this, HandleDetailActivity.class);
                intent.putExtra("matter_id",mDatas.get(position - 1).matter_id);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(HandleListActivity.this);
            }
        });
        listView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if (requestList.size() == StringConstants.PAGENUM && !isLoading) {
                    isLoading = true;
                    page++;
                    getList();
                }
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
        showLoadDialog(1);
        getList();
    }

    @Override
    public void doClick(View v) {
        super.doClick(v);
        switch (v.getId()) {
            case R.id.fg_handle_list_tv_search:

                break;
        }
    }

    private void getList() {
        RequestParams params = new RequestParams(Urls.url_handle_list);
        params.addParameter("section_id",section_id+"");
        params.addParameter("theme_id",theme_id+"");
        params.addParameter("page", page + "");
        params.addParameter("pagenum", StringConstants.PAGENUM);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    Type type = new TypeToken<List<HandleListBean>>() {}.getType();
                    requestList = gson.fromJson(result.data, type);
                    mDatas.addAll(requestList);
                    adapter.notifyDataSetChanged();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
